package com.jxd.service.impl;

import com.jxd.entity.MesContent;
import com.jxd.entity.MesHead;
import com.jxd.hbase.utils.QurryTableUntil;
import com.jxd.model.FaultType;
import com.jxd.repositories.MesHeadRepositories;
import com.jxd.service.FaultTypeMapperService;
import com.jxd.service.FaultTypeService;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IOUtils;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FaultTypeServiceImpl implements FaultTypeService {
    @Autowired
    @Qualifier("mesHeadRepositories")
    MesHeadRepositories mesHeadRepositories;
    @Autowired
    @Qualifier("getHtableByMesHead")
    HTable mesHeadClient;
    @Autowired
    @Qualifier("getHtableByMesContent")
    HTable mesContentClient;
    @Autowired
    private FaultTypeMapperService faultTypeMapper;

    @Override
    /**
     * 获取所有的维护反馈报文
     * "61632","53296","57392","49376"的报文
     * mesid1 61632
     * mesid2 53296
     * mesid3 57392
     * mesid4 49376
     */
    public List<MesHead> getFeedbackMess(String filename, String shipid, String mesid) {
        List<MesHead> feedbackMess = mesHeadRepositories
                .findAllByFileNameAndMesIdAndShipId(
                        filename,
                        mesid,
                        shipid
                );
        return feedbackMess;
    }

    /**
     * 获取故障码
     *
     * @param feedbackMess
     * @return
     */
    @Override
    public List<FaultType> getFaultTypes(List<MesHead> feedbackMess,String tableName,Connection connection) {
        List<String> rowkeys = new ArrayList<>();
        List<MesContent> errorRowkeys = new ArrayList<>();
        List<MesContent> errVal = new ArrayList<>();
        List<MesContent> list = new ArrayList<>();

        for (int i = 0; i < feedbackMess.size(); i++) {
            String rowkey = feedbackMess.get(i).getMesSignId() + "-3";
            rowkeys.add(rowkey);
        }

        List<FaultType> faultTypes = new ArrayList<>();
        QurryTableUntil qurryTableUntil = new QurryTableUntil();
        list = qurryTableUntil.qurryTable(rowkeys, tableName, connection);
        //创建批量查询rowkeys
        for (int i = 0; i < list.size(); i++) {
            String mesDate = feedbackMess.get(i).getMesDate();
            if (!list.get(i).getCode().equals("0")) {
                //!0向下获取对应的代码
                int seq = Integer.valueOf(list.get(i).getCode()) + 3;
                for (int id = 4; id <= seq; id++) {
                    MesContent mesContent = new MesContent();
                    String errorRowkey
                            = list.get(i).getMesSignId() + id;
                    mesContent.setRowkey(errorRowkey);
                    mesContent.setMesDate(mesDate);
                    errorRowkeys.add(mesContent);
                }

            }
        }
        errVal = qurryTableUntil.qurryTimeAndVal(errorRowkeys, tableName, connection);
        for (int i = 0; i < errVal.size(); i++) {
            FaultType faultType = new FaultType();
            //设置错误代码
            faultType.setGpgzm(errVal.get(i).getCode());
            //默认1
            faultType.setLastdatadate(errVal.get(i).getMesDate());
            faultType.setGzcs("1");
            faultTypes.add(faultType);
        }
        return faultTypes;
    }

    @Override
    /**
     * 回写入oracle
     */
    public void insertFaultType(List<FaultType> faultTypes, String sbid,
                                String ssdqid,
                                String sysUserdate) {
        for (int i = 0; i < faultTypes.size(); i++) {
            //设置所属地区id
            faultTypes.get(i).setSsdqid(ssdqid);
            //设置船id
            faultTypes.get(i).setSbid(sbid);
            //设置设备使用时间
            faultTypes.get(i).setSysuserdate(sysUserdate);
            faultTypeMapper.insertFaultType(faultTypes.get(i));
        }
    }

}
